#include "comm/znx_time.h"
#include "comm/znx_atomic.h"


int main()
{
    znx_timespec_t  ts;
    int64_t         rs;
    clock_gettime(CLOCK_REALTIME, &ts);
    rs = znx_ts_2_usec(&ts);
    assert(rs == ((int64_t)ts.tv_sec * 1000 * 1000) + ((int64_t)ts.tv_nsec / 1000));
    DD("rs: %lu, ts.sec: %lu, ts.nsec: %lu", rs, ts.tv_sec, ts.tv_nsec);

    znx_usec_2_ts(rs, &ts);
    assert((int64_t)ts.tv_sec == (rs / 1000 / 1000));
    assert((int64_t)ts.tv_nsec == (rs - ((int64_t)ts.tv_sec * 1000 * 1000)) * 1000);
    DD("rs: %lu, ts.sec: %lu, ts.nsec: %lu", rs, ts.tv_sec, ts.tv_nsec);

    int64_t         ms;
    ms = znx_usec_2_msec(rs);
    assert(ms == rs / 1000);
    DD("rs: %lu, ms: %lu", rs, ms);

    int64_t         sec;
    sec = znx_usec_2_sec(rs);
    assert(sec == rs / 1000 / 1000);
    DD("rs: %lu, sec: %lu", rs, sec);
 
    rs = znx_sec_2_usec(sec);
    assert(rs == sec * 1000 * 1000);
    DD("rs: %lu, sec: %lu", rs, sec);

    rs = znx_msec_2_usec(ms);
    assert(rs == ms * 1000);
    DD("rs: %lu, sec: %lu", rs, ms);

    DD("============================================================");

    DD("cache_time: %lu", get_cache_time());

    u_char  http_time_buf[ZNX_TIME_HTTP_TIME_SIZE];
    u_char  log_time_buf[ZNX_TIME_LOG_TIME_SIZE];

    int     len;
    len = znx_cache_http_time(http_time_buf, ZNX_TIME_HTTP_TIME_SIZE);
    DD("len: %d, http_time: %s", len, http_time_buf);

    len = znx_cache_log_time(log_time_buf, ZNX_TIME_LOG_TIME_SIZE);
    DD("len: %d, log_time: %s", len, log_time_buf);


    int64_t time = znx_parse_http_time((u_char *)"Mon, 29 May 2023 02:11:47 GMT",
        strlen("Mon, 29 May 2023 02:11:47 GMT"));
    if (time < 0) {
        DD_FATAL("expect time >= 0");
    }

    DD("time: %ld", time);

    time = znx_parse_http_time((u_char *)"Mon, 29-May-23 02:11:47 GMT",
        strlen("Mon, 29-May-23 02:11:47 GMT"));
    if (time < 0) {
        DD_FATAL("expect time >= 0");
    }

    DD("time: %ld", time);

    time = znx_parse_http_time((u_char *)"Mon May  29 02:11:47 2023",
        strlen("Mon May  29 02:11:47 2023"));

    if (time < 0) {
        DD_FATAL("expect time >= 0");
    }

    DD("time: %ld", time);

    return 0;
}